from django.shortcuts import render, HttpResponse, redirect
from app01.models import UserInfo, ScheduleInfo

# Create your views here.

def index(request):
    return render(request, "index.html")


def user_list(request):
    # 如果在DIRs中配置os.path.join(BASE_DIR, 'templates')，就会优先去根目录templates寻找
    # 默认去app目录下的templates目录寻找user_list.html
    #（根据app的注册顺序，逐一去他们的templates目录中寻找）
    return render(request, "user_list.html")


def user_add(request):
    return HttpResponse("添加用户")


def tpl(request):
    name = "韩超"
    roles = ["管理员", "ceo", "保安"]
    user_info = {"name":"郭泽", "salary":1000, "role":"cto"}
    data_list = [
        {"name":"郭泽1", "salary":1000, "role":"cto"},
        {"name":"郭泽2", "salary":1000, "role":"cto"},
        {"name":"郭泽3", "salary":1000, "role":"cto"}
    ]
    return render(request, 'tpl.html', {"n1":name, "n2":roles, "n3":user_info, "n4":data_list })


def news(request):
    # 1、定义一些新闻（字典或列表）或去数据库，网络请求去联通新闻
    # 向地址http://www.chinaunicom.com.cn/api/article/NewsByIndex/2/2022/11/news发生请求
    import requests
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) \
         AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.42'}
    res = requests.get("http://www.chinaunicom.com.cn/api/article/NewsByIndex/2/2022/11/news", headers=headers)
    data_list = res.json()
    print(data_list)

    return render(request, 'news.html', {"news_list":data_list})


def something(request):
    # request是一个对象，封装了用户发送过来的所有请求相关数据

    # 1.获取请求方式 GET/POST
    print(request.method)

    # 2.在URL上传递值 /something/?n1=123&n2=999
    print(request.GET)

    # 3.在请求体中提交数据
    print(request.POST)

    # 4.【响应】HttpResponse("返回内容")，内容字符串内容返回给请求者。
    # return HttpResponse("返回内容")

    # 5.【响应】读取HTML的内容 + 渲染（替换） -> 字符串，返回给用户浏览器。
    # return render(request, 'something.html', {"title": "来了"})

    # 6.【响应】让浏览器重定向到其他的页面
    return redirect("https://www.baidu.com")


def login(request):
    if request.method == "GET":
        return render(request, "login.html")
    else:
        print(request.POST)
        username = request.POST.get("user")
        password = request.POST.get("password")
        if username == 'root' and password == '123':
            return redirect("http://www.chinaunicom.com.cn/")
            # return HttpResponse("登录成功")
        else:
            # return HttpResponse("登录失败")
            return render(request, 'login.html', {"error_msg":"用户名或密码错误"})


def orm(request):
    #### 1.新建 ####
    # Department.objects.create(title="销售部")
    # Department.objects.create(title="IT部")
    # Department.objects.create(title="运营部")
    # UserInfo.objects.create(name="武沛齐", password="123", age=19)
    # UserInfo.objects.create(name="朱虎飞", password="666", age=29)
    # UserInfo.objects.create(name="吴阳军", password="666", age=18)
    # return HttpResponse("添加成功")
    # #### 2.删除 ####
    # UserInfo.objects.filter(id=3).delete()
    # Department.objects.all().delete()

    # #### 3.获取数据 ####
    # 3.1 获取符合条件的所有数据
    # data_list = [对象,对象,对象]  QuerySet类型
    # data_list = UserInfo.objects.all()
    # for obj in data_list:
    #     print(obj.id, obj.name, obj.password, obj.age)

    # data_list = [对象,]
    # data_list = UserInfo.objects.filter(id=1)
    # print(data_list)
    # 3.1 获取第一条数据【对象】
    # row_obj = UserInfo.objects.filter(id=1).first()
    # print(row_obj.id, row_obj.name, row_obj.password, row_obj.age)


    # #### 4.更新数据 ####
    # UserInfo.objects.all().update(password=999)
    # UserInfo.objects.filter(id=2).update(age=999)
    # UserInfo.objects.filter(name="朱虎飞").update(age=999)
    pass


def info_list(request):
    # 1.获取数据库中的所有用户信息
    data_list = UserInfo.objects.all()
    return render(request, "info_list.html", {"data_list":data_list})


def info_add(request):
    if request.method == "GET":
        return render(request,"info_add.html")

    # 获取用户提交的数据
    user = request.POST.get("user")
    password = request.POST.get("password")
    age = request.POST.get("age")

    # 添加到数据库
    UserInfo.objects.create(name=user, password=password, age=age)

    # 自动跳转
    return redirect("/info/list")


def info_delete(request):
    nid = request.GET.get('nid')
    UserInfo.objects.filter(id=nid).delete()
    return redirect("/info/list/")


